package com.example;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class Activity1 extends AppCompatActivity {

    private static final int request = 100;
    private static final int resultOne = 200;
    private static final int resultTwo = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

        Button button_1 = (Button)findViewById(R.id.button_1);
        Button button_2 = (Button)findViewById(R.id.button_2);
        button_1.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
//                发数据
                String data = "hello";
                Intent intent = new Intent(Activity1.this, Activity2.class);
                intent.putExtra("extra_data",data);
//                接收数据
                startActivityForResult(intent,request);
            }
        });

        button_2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
//                发数据
                String data = "hello";
                Intent intent = new Intent("com.example.Activity3");
                intent.putExtra("extra_data",data);
//                接收数据
                startActivityForResult(intent,request);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        Log.d("msg","进入了");
//        Log.d("rqCode",String.valueOf(requestCode));
//        Log.d("code",String.valueOf(resultCode));
        if(resultCode == resultOne){
            if(data != null){
                String data_return = data.getStringExtra("data_return");
                Log.d("Activity1", data_return);
            }
        }
        if(resultCode == resultTwo){
            if(data != null){
                String data_return = data.getStringExtra("data_return");
                Log.d("Activity1", data_return);
            }
        }
    }

}